//
//  NSDictionary+SortKeys.m
//  TUIKitCategory
//
//  Created by txj on 2017/3/14.
//  Copyright © 2017年 hs. All rights reserved.
//

#import "NSDictionary+Extra.h"

@implementation NSDictionary(Extra)

-(NSString *)strDateForKey:(NSString *)strKey andType:(NSString *)type
{
    return [[self stringValueForKey:strKey] strDate:type];
}

-(NSString *)stringValueForKey:(NSString *)strKey
{
    id obj=[self objectForKey:strKey];
    if ([obj isKindOfClass:[NSString class]]) {
        return [NSString stringValue:obj];
    }
    if ([obj isKindOfClass:[NSNumber class]]) {
        return [NSString stringValue:[obj stringValue]];
    }
    return [NSString stringValue:[self objectForKey:strKey]];
}

-(NSString *)sortByKeys:(BOOL)bEncoded
{
    NSArray* sortedKeyArray = [[self allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    NSMutableArray *tmpArray = [NSMutableArray new];
    for (NSString* key in sortedKeyArray) {
        NSString* orderItem = [self orderItemWithKey:key andValue:[self objectForKey:key] encoded:bEncoded];
        if (orderItem.length > 0) {
            [tmpArray addObject:orderItem];
        }
    }
    //    return [tmpArray componentsJoinedByString:@"&"];
    return [tmpArray componentsJoinedByString:@""];
    
/////////########///以下会过滤空字符/////////////
//    NSArray* sortedKeyArray = [[self allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
//        return [obj1 compare:obj2];
//    }];
//    NSMutableArray *tmpArray = [NSMutableArray new];
//    for (NSString* key in sortedKeyArray) {
//        NSString* orderItem = [self orderItemWithKey:key andValue:[self objectForKey:key] encoded:bEncoded];
//        if (orderItem.length > 0) {
//            [tmpArray addObject:orderItem];
//        }
//    }
////    return [tmpArray componentsJoinedByString:@"&"];
//    return [tmpArray componentsJoinedByString:@""];
}
- (NSString*)orderItemWithKey:(NSString*)key andValue:(NSString*)value encoded:(BOOL)bEncoded
{
//    if (key.length > 0 && value.length > 0) {
        if (bEncoded) {
            value = [self encodeValue:value];
        }
        //        return [NSString stringWithFormat:@"%@=%@", key, value];
        return [NSString stringWithFormat:@"%@%@", key, value];
//    }
//    return nil;
    
    
//    /////////########///以下会过滤空字符/////////////
//    if (key.length > 0 && value.length > 0) {
//        if (bEncoded) {
//            value = [self encodeValue:value];
//        }
////        return [NSString stringWithFormat:@"%@=%@", key, value];
//        return [NSString stringWithFormat:@"%@%@", key, value];
//    }
//    return nil;
}
- (NSString*)encodeValue:(NSString*)value
{
    NSString* encodedValue = value;
    if (value.length > 0) {
        encodedValue = (__bridge_transfer  NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)value, NULL, (__bridge CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
    }
    return encodedValue;
}

@end
